package com.client.act;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.client.bean.BirdMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.common.ToastUtil;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.client.task.AddBirdMessagesTask;
import com.client.task.EditBirdMessagesTask;
import com.clinet.cache.AppConstants;
import com.clinet.cache.AsyncImageLoader;
import com.clinet.cache.Log;

public class DetaiAct extends BaseAct {

	private Context mContext = DetaiAct.this;
	public static AsyncImageLoader mAsyncImageLoader = null;

	public final static int EDITMODE = 0;
	public final static int ADDMODE = 1;
	public static int MODE = -1;

	private DataService mDataService = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		initBottomBar();
		init();
		onclick();
		switch (MODE) {
		case EDITMODE:
			Log.i("------------EDITMODE--------");
			getDataEDIT();
			setValue();
			break;
		case ADDMODE:
			Log.i("------------ADDMODE--------");
			getDataADD();
			setValueForAdd();
//			setValue();
//			sickNum.setFocusable(true);
//			sickDes.setFocusable(false);
//			deadDes.setFocusable(false);
			break;
		default:
			Log.i("------------ADDMODE--------");
			getDataADD();
			setValueForAdd();
//			sickNum.setFocusable(true);
//			sickDes.setFocusable(false);
//			deadDes.setFocusable(false);
			break;
		}
		// getValue();
	
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isSave) {
			switch (CURRENT) {
			case SICKIMAGE:
				Bitmap mBitmap = (Bitmap) ValueStack.getInstance().getValue(
						"bitmap");
				Log.i("-------bitmap-----" + mBitmap);
				Log.i("-------bitmap-----" + mBitmap.isRecycled());
				String desS = (String) ValueStack.getInstance()
						.getValue("desS");
				sickImageImageView.setImageBitmap(mBitmap);
				bean.sickImage = savePicToSdcard(mBitmap);
				sickDes.setText(desS);
				CURRENT=-1;
				break;
			case DEADIMAGE:
				Bitmap mBitmap1 = (Bitmap) ValueStack.getInstance().getValue(
						"bitmap");
				String desS1 = (String) ValueStack.getInstance().getValue(
						"desS");
				Log.i("-------bitmap-----" + mBitmap1);
				Log.i("-------bitmap-----" + mBitmap1.isRecycled());
				deadImageImageView.setImageBitmap(mBitmap1);
				bean.deadImage = savePicToSdcard(mBitmap1);
				deadDes.setText(desS1);
				CURRENT=-1;
				break;
			case SEARCHKEY:
				String birdNames = (String) ValueStack.getInstance().getValue(
						"birdName");
				birdName.setText(birdNames);
				CURRENT=-1;
				break;
			}
		}

	}

	private String savePicToSdcard(Bitmap bitmap) {
		String path = AppConstants.imageCachePath + UUID.randomUUID() + ".jpg";
		Log.i("savePicToSdcard-----" + path);
		File file = new File(path);
		FileOutputStream outputStream;
		// Log.i("test", "file.exists() : "+file.exists());
		if (!file.exists()) {
			file.delete();
		}
		try {
			file.getParentFile().mkdirs();
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			// Log.i("test", path+picName);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
			outputStream.close();
		} catch (Exception e) {
			// Log.e("", e.toString());
		}
		return path;
	}

	private BirdMessage bean = null;

	private void getDataEDIT() {
		// bean = new BirdMessage();
		// ValueStack.getInstance().setValue("bean", birdMessage);
		bean = (BirdMessage) ValueStack.getInstance().getValue("bean");
	
	}
	
    

	private void getDataADD() {
		bean = new BirdMessage();
		// ValueStack.getInstance().setValue("bean", birdMessage);
		java.util.Date current=new java.util.Date();
	    java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy 年 MM月 dd日"); 
	    String c=sdf.format(current);
	    bean.date=c;

	}

	private AlertDialog statusDialog = null;

	private String[] statukind = new String[] { "取食", "饮水", "休息", "飞行", "筑巢",
			"争斗", "育幼", "其他" };

	private boolean[] check = { false, false, false, false, false, false,
			false, false };
	private boolean[] check2 = { false, false, false, false, false, false,
			false, false };

	private void openMultStatus() {
		String[] temps = bean.status.trim().split(",");
		for (int i = 0; i < temps.length; i++) {
			for (int j = 0; j < statukind.length; j++) {
				if (temps[i].equals(statukind[j])) {
					check[j] = true;
					check2[j] = true;
				}

			}
		}
		statusDialog = new AlertDialog.Builder(this)
				.setTitle("状态选择")
				.setMultiChoiceItems(statukind, check,
						new OnMultiChoiceClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which, boolean isChecked) {
								check2[which] = isChecked;

							}
						})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						StringBuffer mStringBuffer = new StringBuffer();
						String temp = "";
						for (int j = 0; j < check2.length; j++) {
							if (check2[j]) {
								mStringBuffer.append(statukind[j]);
								mStringBuffer.append(",");
							}
						}
						if (mStringBuffer.length() > 0) {
							temp = mStringBuffer.substring(0,
									(mStringBuffer.lastIndexOf(",")));
						}
						bean.status = temp;
						status.setText(bean.status);
						Log.i("DetaiAct.openMultStatus()-------------"
								+ bean.status);

					}

				}).setNegativeButton("取消", null).create();
		statusDialog.show();
	}

	/**
	 * 对话框
	 */
	private String[] items = new String[] { "游禽", "涉禽", "猛禽", "陆禽", "攀禽", "鸣禽" };
	private AlertDialog kindDialog = null;

	private void openKingDialog() {
		kindDialog = new AlertDialog.Builder(mContext).setTitle("种类选择")
				.setItems(items, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						kind.setText(items[which]);
					}

				}).create();
		kindDialog.show();
	}

	private ImageView search = null;
	private ImageView save = null;
	private ImageView upload = null;
	private ImageView back = null;

	private void onclick() {
		birdImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				openCapture(birdImage, null, BIRDIMAGE);
			}
			
		});

		search = (ImageView) findViewById(R.id.search);
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, NameSearchAct.class);
				mContext.startActivity(intent);
				CURRENT = SEARCHKEY;
				isSave = false;
			}
		});

		save = (ImageView) findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updateLocalData();
			}
		});

		upload = (ImageView) findViewById(R.id.upload);
		upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				connectNet();

			}

			

		});
		back = (ImageView) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getValue();
				mDataService.updateBirdMessage(bean);
				finish();
			}
		});
		kindImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openKingDialog();
			}
		});
		statusImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openMultStatus();
			}
		});
		sickImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// sickImageImageView.setText(bean.sickImageImageView);
				// sickDes.setFocusable(true);
				openCapture(sickImageImageView, sickDes, SICKIMAGE);
			}

		});

		deadImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// deadImageImageView.setText(bean.deadImageImageView);
				// deadDes.setFocusable(true);
				openCapture(deadImageImageView, deadDes, DEADIMAGE);
			}
		});

		collectLoactionImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openCollectDialog();
				// collectLoaction.setText(bean.collectLoaction);
			}
		});

		placeImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openPlaceDialog();
				// place.setText(bean.collectLoaction);
			}
		});

		directImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openDirectDialog();
				// direct.setText(bean.direct);
			}
		});

		distanceImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openDistancesDialog();
				// distanceImageView.setText(bean.distanceImageView);
			}
		});

		stayPlaceImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openStayPlacesDialog();
				// stayPlace.setText(bean.stayPlace);
			}
		});
	}
	/**
	 * 
	 */
	private void connectNet() {
		getValue();
		SpinnerProgressDialog.showDialog(mContext, "正在上传....");
		if("".equals(bean.birdId))
		{
			addNet();
		}else
		{
			updateNet();
		}
//		switch (MODE) {
//		case EDITMODE:
//			updateNet();
//			break;
//		case ADDMODE:
//			addNet();
//			break;
//		}
	}
	// 先进行上传成功就更新id
	AddBirdMessagesTask.Callback callback = new AddBirdMessagesTask.Callback() {

		@Override
		public void onComplete(Integer result, int position) {
			SpinnerProgressDialog.dismissDialog();
			switch (result) {
			case 1:
				ToastUtil.toast(mContext, "上传成功");
				finish();
				break;
			case 0:
				ToastUtil.toast(mContext, "上传失败");
				break;
			}
		}

	};
	//
	AddBirdMessagesTask mAddBirdMessagesTask =null;
	private void addNet() {
		if(mAddBirdMessagesTask!=null&&mAddBirdMessagesTask.isIsrunning())
		{
			ToastUtil.toast(mContext, "正在上传....");
			return;
		}
		 mAddBirdMessagesTask = new AddBirdMessagesTask(
				mContext, callback, mDataService);
		mAddBirdMessagesTask.execute(bean);

	}

	// 先进行上传成功就更新id
	EditBirdMessagesTask.Callback callback2 = new EditBirdMessagesTask.Callback() {
		@Override
		public void onComplete(Integer result, int position) {
			switch (result) {
			case 1:
				ToastUtil.toast(mContext, "上传成功");
				finish();
				break;
			case 0:
				ToastUtil.toast(mContext, "上传失败");
				break;
			}
		}

	};
	EditBirdMessagesTask mEditBirdMessagesTask=null;
	private void updateNet() {
		if(mEditBirdMessagesTask!=null&&mEditBirdMessagesTask.isIsrunning())
		{
			ToastUtil.toast(mContext, "正在上传....");
			return;
		}
		//
		 mEditBirdMessagesTask = new EditBirdMessagesTask(
				mContext, callback2, mDataService);
		mEditBirdMessagesTask.execute(bean);

	}

	private void openCapture(ImageView sickImageImageView, EditText sickDes,
			int resultCode) {
		switch (resultCode) {
		case BIRDIMAGE:
			Intent intent3 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intent3, BIRDIMAGE);
			break;
		case SICKIMAGE:
			Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intent1, SICKIMAGE);
			break;
		case DEADIMAGE:
			Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intent2, DEADIMAGE);
			break;
		}

	}

	public static final int SICKIMAGE = 0;
	public static final int DEADIMAGE = 1;
	public static final int BIRDIMAGE = 3;
	public static final int SEARCHKEY = 2;
	public static int CURRENT = -1;

	public static boolean isSave = false;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Log.i("DetaiAct.onActivityResult requestCode  " + requestCode
				+ "resultCode" + resultCode);
		if (resultCode == RESULT_OK) {
			if (data != null) {
				final Bitmap photo = data.getParcelableExtra("data");
				if (photo != null) {
					switch (requestCode) {
					
					case BIRDIMAGE:
						// 选择图片
						birdImage.setImageBitmap(photo);
						//生成图片
						bean.birdImage=savePicToSdcard(photo);
						break;
					case SICKIMAGE:
						// 选择图片
						Intent mIntent = new Intent(mContext, EidtPicAct.class);
						ValueStack.getInstance().setValue("bitmap", photo);
						CURRENT = SICKIMAGE;
						isSave = false;
						startActivity(mIntent);
						break;
					case DEADIMAGE:
						Intent mIntent2 = new Intent(mContext, EidtPicAct.class);
						ValueStack.getInstance().setValue("bitmap", photo);
						CURRENT = DEADIMAGE;
						isSave = false;
						startActivity(mIntent2);
						break;
					}
				}
			} else {
				Toast.makeText(mContext, "请重新选择图片", Toast.LENGTH_SHORT).show();
			}

		}

	}

	private String[] collectLoactions = new String[] { "山顶", "山腰", "山谷", "平地",
			"其他" };
	private AlertDialog collectDialog = null;

	private void openCollectDialog() {
		collectDialog = new AlertDialog.Builder(mContext)
				.setTitle("采集地点")
				.setItems(collectLoactions,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								collectLoaction
										.setText(collectLoactions[which]);
							}

						}).create();
		collectDialog.show();
	}

	private String[] places = new String[] { "山顶", "山腰", "山谷", "平地", "其他" };
	private AlertDialog placeDialog = null;

	private void openPlaceDialog() {
		placeDialog = new AlertDialog.Builder(mContext).setTitle("位置选择")
				.setItems(places, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						place.setText(places[which]);
					}

				}).create();
		placeDialog.show();
	}

	private String[] directs = new String[] { "东", "南", "西", "北", "东南", "东北",
			"西南", "西北" };
	private AlertDialog directsDialog = null;

	private void openDirectDialog() {
		directsDialog = new AlertDialog.Builder(mContext).setTitle("方位选择")
				.setItems(directs, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						direct.setText(directs[which]);
					}

				}).create();
		directsDialog.show();
	}

	private String[] distances = new String[] { "0~50米", "50~150米", "150~250米" };
	private AlertDialog distancessDialog = null;

	private void openDistancesDialog() {
		distancessDialog = new AlertDialog.Builder(mContext).setTitle("距离选择")
				.setItems(distances, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						distance.setText(distances[which]);
					}

				}).create();
		distancessDialog.show();
	}

	private String[] stayPlaces = new String[] { "林地", "灌丛", "农田", "水域", "果园",
			"其他", "城市公园", "城市绿地" };
	private AlertDialog stayPlacesDialog = null;

	private void openStayPlacesDialog() {
		stayPlacesDialog = new AlertDialog.Builder(mContext).setTitle("栖息地选择")
				.setItems(stayPlaces, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						stayPlace.setText(stayPlaces[which]);
					}

				}).create();
		stayPlacesDialog.show();
	}

	private boolean isEdit = false;

	private void setValue() {
		// birdImage.setText(bean.birdImage);
		Log.i("setValue"+bean.toString());
		mAsyncImageLoader.getLogoItem(mContext, bean.birdImage, birdImage);
		date.setText(bean.date);
		birdName.setText(bean.birdName);
		birdName.setFocusable(isEdit);
		num.setText(bean.num);
		// num.setFocusable(true);
		kind.setText(bean.kind);
		kind.setFocusable(isEdit);

		// kindImageView.setText(bean.kindImageView);
		healthNum.setText(bean.healthNum);
		// healthNum.setFocusable(true);
		status.setText(bean.status);
		status.setFocusable(isEdit);
		// statusImageView.setText(bean.statusImageView);
		sickNum.setText(bean.sickNum);
		sickNum.setFocusable(true);
		mAsyncImageLoader.getLogoItem(mContext, bean.sickImage,
				sickImageImageView);
		// sickImage.setText(bean.sickImage);
		// sickImageImageView.setText(bean.sickImageImageView);
		sickDes.setText(bean.sickDes);
		sickDes.setFocusable(false);
		// sickDes.setFocusable(true);
		deadNum.setText(bean.deadNum);
		deadNum.setFocusable(true);
		mAsyncImageLoader.getLogoItem(mContext, bean.deadImage,
				deadImageImageView);
		// deadImage.setText(bean.deadImage);
		// deadImageImageView.setText(bean.deadImageImageView);
		deadDes.setText(bean.deadDes);
		deadDes.setFocusable(false);
		// deadDes.setFocusable(true);
		collectLoaction.setText(bean.collectLoaction);
		// collectLoaction.setFocusable(isEdit);

		// collectLoactionImageView.setText(bean.collectLoactionImageView);
		place.setText(bean.place);
		place.setFocusable(isEdit);
		// placeImageView.setText(bean.placeImageView);
		direct.setText(bean.direct);
		direct.setFocusable(isEdit);
		// directImageView.setText(bean.directImageView);
		distance.setText(bean.distance);
		distance.setFocusable(isEdit);
		// distanceImageView.setText(bean.distanceImageView);
		stayPlace.setText(bean.stayPlace);
		stayPlace.setFocusable(isEdit);
		// stayPlaceImageView.setText(bean.stayPlaceImageView);

	}
	private void setValueForAdd() {
		// birdImage.setText(bean.birdImage);
		mAsyncImageLoader.getLogoItem(mContext, bean.birdImage, birdImage);
		date.setText(bean.date);
		birdName.setText(bean.birdName);
		birdName.setFocusable(isEdit);
		// num.setFocusable(true);
		kind.setText(bean.kind);
		kind.setFocusable(isEdit);

		// kindImageView.setText(bean.kindImageView);
		// healthNum.setFocusable(true);
		status.setText(bean.status);
		status.setFocusable(isEdit);
		// statusImageView.setText(bean.statusImageView);
//		sickNum.setText(bean.sickNum);
		mAsyncImageLoader.getLogoItem(mContext, bean.sickImage,
				sickImageImageView);
		// sickImage.setText(bean.sickImage);
		// sickImageImageView.setText(bean.sickImageImageView);
		sickDes.setText(bean.sickDes);
		 sickDes.setFocusable(false);
//		deadNum.setText(bean.deadNum);
		mAsyncImageLoader.getLogoItem(mContext, bean.deadImage,
				deadImageImageView);
		// deadImage.setText(bean.deadImage);
		// deadImageImageView.setText(bean.deadImageImageView);
		deadDes.setText(bean.deadDes);
		deadDes.setFocusable(false);
		// deadDes.setFocusable(true);
		collectLoaction.setText(bean.collectLoaction);
		// collectLoaction.setFocusable(isEdit);

		// collectLoactionImageView.setText(bean.collectLoactionImageView);
		place.setText(bean.place);
		
		place.setFocusable(false);
		// placeImageView.setText(bean.placeImageView);
		direct.setText(bean.direct);
		direct.setFocusable(isEdit);
		// directImageView.setText(bean.directImageView);
		distance.setText(bean.distance);
		distance.setFocusable(isEdit);
		// distanceImageView.setText(bean.distanceImageView);
		stayPlace.setText(bean.stayPlace);
		stayPlace.setFocusable(isEdit);
		// stayPlaceImageView.setText(bean.stayPlaceImageView);

	}
	private void getValue() {

		bean.date = date.getText().toString().trim();
		bean.birdName = birdName.getText().toString().trim();
		bean.num = num.getText().toString().trim();
		if("".equals(bean.num))
		{
			bean.num="0";
		}
		bean.kind = kind.getText().toString().trim();
		bean.healthNum = healthNum.getText().toString().trim();
		if("".equals(bean.healthNum))
		{
			bean.healthNum="0";
		}
		bean.status = status.getText().toString().trim();
		bean.sickNum = sickNum.getText().toString().trim();
		if("".equals(bean.sickNum))
		{
			bean.sickNum="0";
		}
		bean.sickDes = sickDes.getText().toString().trim();
		bean.deadNum = deadNum.getText().toString().trim();
		if("".equals(bean.deadNum))
		{
			bean.deadNum="0";
		}
		bean.deadDes = deadDes.getText().toString().trim();
		bean.collectLoaction = collectLoaction.getText().toString().trim();
		bean.place = place.getText().toString().trim();
		bean.direct = direct.getText().toString().trim();
		bean.distance = distance.getText().toString().trim();
		bean.stayPlace = stayPlace.getText().toString().trim();
		Log.i("setValue######"+bean);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		release();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}

	public void release() {
		birdImage = null;
		date = null;
		birdName = null;
		num = null;
		kind = null;
		kindImageView = null;
		healthNum = null;
		status = null;
		statusImageView = null;
		sickNum = null;
		sickImage = null;
		sickImageImageView = null;
		sickDes = null;
		deadNum = null;
		deadImage = null;
		deadImageImageView = null;
		deadDes = null;
		collectLoaction = null;
		collectLoactionImageView = null;
		place = null;
		placeImageView = null;
		direct = null;
		directImageView = null;
		distance = null;
		distanceImageView = null;
		stayPlace = null;
		stayPlaceImageView = null;
	}

	private ImageView birdImage = null;
	private TextView date = null;
	private EditText birdName = null;
	private TextView num = null;
	private TextView kind = null;
	private ImageView kindImageView = null;
	private TextView healthNum = null;
	private EditText status = null;
	private ImageView statusImageView = null;
	private EditText sickNum = null;
	private ImageView sickImage = null;
	private ImageView sickImageImageView = null;
	private EditText sickDes = null;
	private EditText deadNum = null;
	private ImageView deadImage = null;
	private ImageView deadImageImageView = null;
	private EditText deadDes = null;
	private EditText collectLoaction = null;
	private ImageView collectLoactionImageView = null;
	private EditText place = null;
	private ImageView placeImageView = null;
	private EditText direct = null;
	private ImageView directImageView = null;
	private EditText distance = null;
	private ImageView distanceImageView = null;
	private EditText stayPlace = null;
	private ImageView stayPlaceImageView = null;

	private void init() {
		mDataService = new DataService(mContext);
		mAsyncImageLoader = new AsyncImageLoader();
		birdImage = (ImageView) findViewById(R.id.birdImage);
		date = (TextView) findViewById(R.id.date);
		birdName = (EditText) findViewById(R.id.birdName);
		num = (TextView) findViewById(R.id.num);
		kind = (TextView) findViewById(R.id.kind);
		kindImageView = (ImageView) findViewById(R.id.kindImageView);
		healthNum = (TextView) findViewById(R.id.healthNum);
		status = (EditText) findViewById(R.id.status);
		statusImageView = (ImageView) findViewById(R.id.statusImageView);
		sickNum = (EditText) findViewById(R.id.sickNum);
		sickImage = (ImageView) findViewById(R.id.sickImage);
		sickImageImageView = (ImageView) findViewById(R.id.sickImageImageView);
		sickDes = (EditText) findViewById(R.id.sickDes);
		deadNum = (EditText) findViewById(R.id.deadNum);
		deadImage = (ImageView) findViewById(R.id.deadImage);
		deadImageImageView = (ImageView) findViewById(R.id.deadImageImageView);
		deadDes = (EditText) findViewById(R.id.deadDes);
		collectLoaction = (EditText) findViewById(R.id.collectLoaction);
		collectLoactionImageView = (ImageView) findViewById(R.id.collectLoactionImageView);
		place = (EditText) findViewById(R.id.place);
		placeImageView = (ImageView) findViewById(R.id.placeImageView);
		direct = (EditText) findViewById(R.id.direct);
		directImageView = (ImageView) findViewById(R.id.directImageView);
		distance = (EditText) findViewById(R.id.distance);
		distanceImageView = (ImageView) findViewById(R.id.distanceImageView);
		stayPlace = (EditText) findViewById(R.id.stayPlace);
		stayPlaceImageView = (ImageView) findViewById(R.id.stayPlaceImageView);

	}

	private void messageUserSave() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("保存当前新增记录");
		builder.setTitle("友情提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						try {
							bean.uploaded="false";
							mDataService.saveBirdMessage(bean);
							ToastUtil.toast(mContext, "保存成功");
							finish();
							dialog.dismiss();
						} catch (Exception e) {
							ToastUtil.toast(mContext, "保存失败");
						}
						
					}
				});
		builder.setNeutralButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
	private void messageUpdateUserSave() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("数据已改动是否保存");
		builder.setTitle("友情提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						try {
							bean.uploaded="false";
							mDataService.saveBirdMessage(bean);
							ToastUtil.toast(mContext, "保存成功");
							finish();
							dialog.dismiss();
						} catch (Exception e) {
							ToastUtil.toast(mContext, "保存失败");
						}
						
					}
				});
		builder.setNeutralButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = false;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if(!bean.equals(new BirdMessage()))
			{
				messageUpdateUserSave();
			}
			flag = true;
			break;
		}
		return flag;
	}

//	@Override
//	protected void onStop() {
//		super.onStop();
//		messageUpdateUserSave();
//	}
	private void updateLocalData() {
		getValue();
		switch (MODE) {
		case EDITMODE:
			mDataService.updateBirdMessage(bean);
			ToastUtil.toast(mContext, "保存成功");
			finish();
			break;
		case ADDMODE:
			messageUserSave();
			break;
		default:
			getDataADD();
			break;
		}
	}
}